package proyectofinal_diseno;
/**
 * @author Katherine Vargas
 * @author Esteban Herrera
 */
public class DivideVenceras 
{
	static void ejecutar (int N) 
        { 
            int[][]A = new int [N][N]; 
            int [][]B = new int[N][N]; 
            for(int i = 0; i < A.length; i++)
            { 
                for(int j = 0; j < B.length; j++)
                { 
                    A[i][j] = (int)(Math.random()*2); 
                    B[i][j] = A[i][j]; 
		} 
            } 
            //sum(A,B); 
            //rest(A,B); 
            System.out.println("****************************************************"); 
            System.out.println("Imagen Generada: "); 
            imprime(A);
            /*int[][]multResult = mult(A,B); 
            imprime(multResult); 
            System.out.println("****************************************************"); 
            */System.out.println(); 
            GeneraStrassen(A,B); 
	} 
	public static int[][] sum(int[][]A,int [][]B)
        { 
            int[][]C = new int[A.length][B.length]; 
            for(int i = 0; i <A.length;i++)
            { 
                for(int j = 0; j < B.length;j++)
                { 
                    C[i][j] = A[i][j] + B[i][j]; 
		} 
            } 
            //imprime(C); 
            return C; 
	} 
	
        public static int[][] rest(int[][]A,int [][]B)
        { 
            int[][]C = new int[A.length][B.length]; 
            for(int i = 0; i < A.length;i++)
            { 
                for(int j = 0; j < B.length;j++)
                { 
                    C[i][j] = A[i][j] - B[i][j]; 
		} 
            } 
            //imprime(C); 
            return C; 
	} 
	
        public static int[][] mult(int[][]A,int [][]B)
        { 
            int[][]C = new int[A.length][A.length]; 
            for(int i = 0; i < C.length; i++)
            { 
                for(int j = 0; j < C.length;j++)
                {
                    for(int k = 0; k < C.length; k++)
                    { 
                        C[i][j] += A[i][k] * B[k][j]; 
                    } 
		} 
            } 
            //imprime(C); 
            return C; 
	} 
                
	public static void imprime(int[][]array)
        { 
            for(int i = 0; i < array.length; i++)
            { 
                for(int j= 0; j < array.length; j++)
                { 
                    System.out.print(array[i][j] + " "); 
		} 
                    System.out.println(); 
            } 
	} 
        
        private static int[][] rotar(int[][] a) 
        {
            int[][] b = new int[a.length][a[0].length];
            int rota = 3;
            
            for (int i = 0; i < rota; ++i) 
            {
                for (int j = 0; j < rota; ++j) 
                {
                    b[i][j] = a[(rota - 1) - j][i];
                }
            }
            return b;
        }
        
        public static void GeneraStrassen(int[][]A, int[][]B)
        { 
            int mitad = A.length/2; 
            int[][]A11 = new int[mitad][mitad]; 
            int[][]A12 = new int[mitad][mitad]; 
            int[][]A21 = new int[mitad][mitad]; 
            int[][]A22 = new int[mitad][mitad]; 
            int[][]B11 = new int[mitad][mitad]; 
            int[][]B12 = new int[mitad][mitad]; 
            int[][]B21 = new int[mitad][mitad]; 
            int[][]B22 = new int[mitad][mitad]; 
            for(int i = 0; i < mitad; i++)
            { 
                for(int j = 0; j < mitad; j++)
                { 
                    A11[i][j] = A[i][j]; 
                    B11[i][j] = B[i][j]; 
                    A12[i][j] = A[i][mitad+j]; 
                    B12[i][j] = B[i][mitad+j]; 
                    A21[i][j] = A[mitad+i][j]; 
                    B21[i][j] = B[mitad+i][j]; 
                    A22[i][j] = A[mitad+i][mitad+j]; 
                    B22[i][j] = B[mitad+i][mitad+j]; 
                    
		} 
            } 
            
            int[][]M1 = mult((sum(A11,A22)),(sum(B11,B22))); 
            int[][]M2 = mult((sum(A21,A22)),(B11)); 
            int[][]M3 = mult((A11),(rest(B12,B22))); 
            int[][]M4 = mult((A22),(rest(B21,B11))); 
            int[][]M5 = mult((sum(A11,A12)),(B22)); 
            int[][]M6 = mult((rest(A21,A11)),(sum(B11,B12))); 
            int[][]M7 = mult((rest(A12,A22)),(sum(B21,B22))); 
            
            int[][]C11 = sum((sum(M1,rest(M4,M5))),M7); 
            int[][]C12 = sum(M3,M5); 
            int[][]C21 = sum(M2,M4); 
            int[][]C22 = sum((sum(rest(M1,M2),M3)),M6); 
            int[][]C = new int[A.length][A[0].length]; //0
            
            for(int i = 0; i<mitad; i++)
            { 
                for(int j= 0; j<mitad;j++)
                { 
                    C[i][j] = C11[i][j];
                    C[i][mitad+j] = C12[i][j]; 
                    C[mitad+i][j] = C21[i][j];  
                    C[mitad+i][mitad+j] = C22[i][j];  
		} //rotar(C);
            } 
            System.out.println("****************************************************"); 
            System.out.println("Rotando la Imagen 90 Grados: \n\n Rotando...\n\n"); 
            imprime(rotar(C)); 
            System.out.println("****************************************************"); 
            System.exit(0);
	} 
}